package com.wing.res.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.res.model.entity.ResProductMoldDraw;
import com.wing.res.model.request.ResDrawVersionUpdateForm;
import com.wing.res.model.response.ResProductMoldDrawVo;
import com.wing.res.service.ResProductMoldDrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * sku图纸信息
 * @version 1.1.0
 * @Author hz
 * @Date 2024-10-06 10:46
 **/
@Slf4j
@Validated
@Api(value = "产品模具图纸信息", tags = "#/res/draw/proMoldDraw")
@RestController("resPlanMoldDrawController")
@RequestMapping("res/admin/proMoldDraw")
public class ResProductMoldDrawController {

    /**
     * 模具图纸表
     */
    @Resource
    private ResProductMoldDrawService resProductMoldDrawService;

    /**
     *查看图纸 以及他的历史版本
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param status
     * @param isDefault
     * @param code
     * @param name
     * @param skuCode
     * @param skuNo
     * @return
     */
    @ApiOperation(value = "查看sku图纸列表", notes = "搜索-图纸")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ResProductMoldDrawVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String status
            ,String isDefault
            ,String code
            ,String name
            ,Long drawId
            ,String skuCode
            ,String skuNo
    ) {
        Page<ResProductMoldDrawVo> pg = new Page<>(page, limit);
        Map<String,Object> map = new HashMap();
        map.put("status", status);
        map.put("isDefault", isDefault);
        map.put("code", code);
        map.put("name", name);
        map.put("drawId", drawId);
        map.put("skuCode", skuCode);
        map.put("skuNo", skuNo);
        Page<ResProductMoldDrawVo> list = resProductMoldDrawService.getListByMap(pg,map);
        list.getRecords().stream().forEach(info->{
            info.setDrawTypeName(MaintenanceEnum.DrawTypeEnum.getNameByCode(info.getDrawType()));
        });
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 作废
     * @param id
     * @return
     */
    @ApiOperation(value = "作废图纸", notes = "图纸-作废")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/cancellation/{id}")
    public JsonResult cancellation(@PathVariable("id") String id) {
        int cancellation = resProductMoldDrawService.cancellation(id);
        if (cancellation > 0) {
            return JsonResult.success("操作成功");
        }else if (cancellation == -1){
            return JsonResult.fail("操作失败! 仅可作废非默认图纸");
        }else{
            return JsonResult.fail("操作失败");
        }
    }


    /**
     * 添加模具图纸
     * @param resProductMoldDraw 模具图纸
     * @return
     */
    @ApiOperation(value = "添加模具图纸", notes = "添加模具图纸")
    @com.wing.web.annotation.RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/addModelDraw")
    public JsonResult addModelDraw(@RequestBody ResProductMoldDraw resProductMoldDraw) {
        int cancellation = resProductMoldDrawService.addModelDraw(resProductMoldDraw);
        if (cancellation > 0) {
            return JsonResult.success("操作成功");
        }else{
            return JsonResult.fail("操作失败");
        }
    }


    /**
     * 添加模具图纸
     * @param resProductMoldDraw 模具图纸
     * @return
     */
    @ApiOperation(value = "修改模具图纸", notes = "修改模具图纸")
    @com.wing.web.annotation.RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PutMapping("/updateModelDraw")
    public JsonResult updateModelDraw(@RequestBody ResProductMoldDraw resProductMoldDraw) {
        int cancellation = resProductMoldDrawService.updateModelDraw(resProductMoldDraw);
        if (cancellation > 0) {
            return JsonResult.success("操作成功");
        }else{
            return JsonResult.fail("操作失败");
        }
    }

    /**
     * 恢复
     * @param id
     * @return
     */
    @ApiOperation(value = "恢复图纸", notes = "图纸-恢复")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/recover/{id}")
    public JsonResult recover(@PathVariable("id") String id) {
        int cancellation = resProductMoldDrawService.recover(id);
        if (cancellation > 0) {
            return JsonResult.success("操作成功");
        }else{
            return JsonResult.fail("操作失败");
        }
    }


    /**
     * 设为默认
     * @param id
     * @return
     */
    @ApiOperation(value = "图纸", notes = "图纸-设为默认")
    @com.wing.web.annotation.RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PutMapping("/setDefault/{id}")
    public JsonResult setDefault(@PathVariable("id") String id) {
        int cancellation = resProductMoldDrawService.setDefault(id);
        if (cancellation > 0) {
            return JsonResult.success("操作成功");
        }else{
            return JsonResult.fail("操作失败");
        }
    }


}
